package com.lijian.search.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lijian.common.utils.ExceptionUtil;
import com.lijian.common.utils.TaotaoResult;
import com.lijian.search.dao.SearchDao;
import com.lijian.search.pojo.SearchResult;

/**
 * <p>Title: SearchService.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * @author lijian
 * @date 2017年4月16日下午9:55:11
 * @version 1.0
 */
@Service
public class SearchService implements com.lijian.search.service.SearchService {

	@Autowired
	private SearchDao searchDao;
	/**
	 * <p>Title: search</p>
	 * <p>Description: </p>
	 * @param queryString
	 * @param page
	 * @param rows
	 * @return
	 * @see com.lijian.search.service.SearchService#search(java.lang.String, int, int)
	 */
	@Override
	public SearchResult search(String queryString, int page, int rows) {
		
		SolrQuery query=new SolrQuery();
		try {
			query.setQuery(queryString);
			query.setStart((page-1)*rows);
			query.setRows(rows);
			//设置默认搜索域，注意df是固定的key
			query.set("df", "item_title");
			//设置高亮
			query.setHighlight(true);
			query.addHighlightField("item_title");
			query.setHighlightSimplePre("<font class=\"skcolor_ljg\">");
			query.setHighlightSimplePost("</font>");
			SearchResult result = searchDao.search(query);
			int pageCount=(int) (result.getRecordCount()/rows);
			if(result.getRecordCount()%rows>0){
				pageCount++;
			}
			result.setPageCount(pageCount);
			result.setCurPage(page);
			return result;
		} catch (SolrServerException e) {
			e.printStackTrace();
			return null;
		}
	}

}
